using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
namespace HKB.Data.Access
{
	public sealed class CDBConn
	{
		private bool m_bIsInTransaction;
		private OleDbCommand m_objCommand;
		private OleDbConnection m_objConn;
		private OleDbTransaction m_objTransaction;
		private int m_Timeout;
		public ConnectionState State
		{
			get
			{
				return this.m_objConn.State;
			}
		}
		public int Timeout
		{
			get
			{
				return this.m_Timeout;
			}
			set
			{
				if (value < 1)
				{
					this.m_Timeout = 1;
				}
				else
				{
					this.m_Timeout = value;
				}
			}
		}
		public CDBConn()
		{
			this.m_bIsInTransaction = false;
			this.m_Timeout = 9999;
			this.m_objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("/") + "\\Diy\\data\\vgo.mdb");
			this.m_objConn.Open();
		}
		public CDBConn(bool bOpenNow)
		{
			this.m_bIsInTransaction = false;
			this.m_Timeout = 9999;
			this.m_objConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("/") + "\\Diy\\data\\vgo.mdb");
			if (bOpenNow)
			{
				this.m_objConn.Open();
			}
		}
		public CDBConn(string strConnectionString)
		{
			this.m_bIsInTransaction = false;
			this.m_Timeout = 9999;
			this.m_objConn = new OleDbConnection(strConnectionString);
			this.m_objConn.Open();
		}
		public CDBConn(string strConnectionString, bool bOpenNow)
		{
			this.m_bIsInTransaction = false;
			this.m_Timeout = 9999;
			this.m_objConn = new OleDbConnection(strConnectionString);
			if (bOpenNow)
			{
				this.m_objConn.Open();
			}
		}
		public void BeginTransaction()
		{
			if (!this.m_bIsInTransaction)
			{
				this.m_objCommand = new OleDbCommand();
				this.m_objCommand.Connection = this.m_objConn;
				this.m_objTransaction = this.m_objConn.BeginTransaction();
				if (this.m_Timeout >= 9999)
				{
					this.m_objCommand.CommandTimeout = this.m_Timeout;
				}
				this.m_objCommand.Transaction = this.m_objTransaction;
				this.m_bIsInTransaction = true;
			}
		}
		public void Close()
		{
			if (this.m_objConn.State != ConnectionState.Closed)
			{
				this.m_objConn.Close();
			}
		}
		public void Commit()
		{
			if (this.m_bIsInTransaction)
			{
				this.m_objTransaction.Commit();
				this.m_bIsInTransaction = false;
				this.m_objCommand.Dispose();
			}
		}
		public void Dispose()
		{
			if (this.m_objConn.State != ConnectionState.Closed)
			{
				this.m_objConn.Close();
			}
			this.m_objConn.Dispose();
		}
		public DataSet ExecuteDataSet(string strSQL)
		{
			DataSet dataSet = new DataSet();
			OleDbCommand oleDbCommand = new OleDbCommand(strSQL, this.m_objConn);
			if (this.m_Timeout >= 9999)
			{
				oleDbCommand.CommandTimeout = this.m_Timeout;
			}
			OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
			oleDbDataAdapter.Fill(dataSet);
			oleDbCommand.Dispose();
			oleDbDataAdapter.Dispose();
			return dataSet;
		}
		public DataSet ExecuteDataSet(string strSQL, string strTable)
		{
			DataSet dataSet = new DataSet();
			OleDbCommand oleDbCommand = new OleDbCommand(strSQL, this.m_objConn);
			if (this.m_Timeout >= 9999)
			{
				oleDbCommand.CommandTimeout = this.m_Timeout;
			}
			OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
			oleDbDataAdapter.Fill(dataSet, strTable);
			oleDbCommand.Dispose();
			oleDbDataAdapter.Dispose();
			return dataSet;
		}
		public OleDbDataReader ExecuteReader(string strSQL)
		{
			OleDbCommand oleDbCommand = new OleDbCommand(strSQL, this.m_objConn);
			if (this.m_Timeout >= 9999)
			{
				oleDbCommand.CommandTimeout = this.m_Timeout;
			}
			OleDbDataReader result = oleDbCommand.ExecuteReader();
			oleDbCommand.Dispose();
			return result;
		}
		public void ExecuteSQL(string strSQL)
		{
			if (!this.m_bIsInTransaction)
			{
				OleDbCommand oleDbCommand = new OleDbCommand(strSQL, this.m_objConn);
				if (this.m_Timeout >= 99999)
				{
					oleDbCommand.CommandTimeout = this.m_Timeout;
				}
				oleDbCommand.ExecuteNonQuery();
				oleDbCommand.Dispose();
			}
			else
			{
				this.m_objCommand.CommandText = strSQL;
				this.m_objCommand.ExecuteNonQuery();
			}
		}
		public int FillDataSet(ref DataSet objDataSet, string strSQL)
		{
			OleDbCommand oleDbCommand = new OleDbCommand(strSQL, this.m_objConn);
			if (this.m_Timeout >= 9999)
			{
				oleDbCommand.CommandTimeout = this.m_Timeout;
			}
			OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
			int result = oleDbDataAdapter.Fill(objDataSet);
			oleDbCommand.Dispose();
			oleDbDataAdapter.Dispose();
			return result;
		}
		public int FillDataSet(ref DataSet objDataSet, string strSQL, string strTable)
		{
			OleDbCommand oleDbCommand = new OleDbCommand(strSQL, this.m_objConn);
			if (this.m_Timeout >= 9999)
			{
				oleDbCommand.CommandTimeout = this.m_Timeout;
			}
			OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
			int result = oleDbDataAdapter.Fill(objDataSet, strTable);
			oleDbCommand.Dispose();
			oleDbDataAdapter.Dispose();
			return result;
		}
		~CDBConn()
		{
		}
		public bool Lock(string[] strArrTableName)
		{
			return true;
		}
		public void Open()
		{
			if (this.m_objConn.State != ConnectionState.Open)
			{
				this.m_objConn.Open();
			}
		}
		public void Rollback()
		{
			if (this.m_bIsInTransaction)
			{
				this.m_objTransaction.Rollback();
				this.m_bIsInTransaction = false;
				this.m_objCommand.Dispose();
			}
		}
		public void Update(string strSelectSQL, DataSet objDataSet)
		{
			if (!this.m_bIsInTransaction)
			{
				OleDbCommand oleDbCommand = new OleDbCommand(strSelectSQL, this.m_objConn);
				if (this.m_Timeout >= 9999)
				{
					oleDbCommand.CommandTimeout = this.m_Timeout;
				}
				OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(oleDbCommand);
				OleDbCommandBuilder oleDbCommandBuilder = new OleDbCommandBuilder(oleDbDataAdapter);
				oleDbDataAdapter.InsertCommand = oleDbCommandBuilder.GetInsertCommand();
				oleDbDataAdapter.DeleteCommand = oleDbCommandBuilder.GetDeleteCommand();
				oleDbDataAdapter.UpdateCommand = oleDbCommandBuilder.GetUpdateCommand();
				oleDbDataAdapter.Update(objDataSet);
				oleDbCommandBuilder.Dispose();
				oleDbCommand.Dispose();
				oleDbDataAdapter.Dispose();
			}
			else
			{
				OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(this.m_objCommand);
				OleDbCommandBuilder oleDbCommandBuilder = new OleDbCommandBuilder(oleDbDataAdapter);
				oleDbDataAdapter.InsertCommand = oleDbCommandBuilder.GetInsertCommand();
				oleDbDataAdapter.DeleteCommand = oleDbCommandBuilder.GetDeleteCommand();
				oleDbDataAdapter.UpdateCommand = oleDbCommandBuilder.GetUpdateCommand();
				oleDbDataAdapter.Update(objDataSet);
				oleDbCommandBuilder.Dispose();
				oleDbDataAdapter.Dispose();
			}
		}
	}
}
